package cn.iocoder.yudao.module.distm.controller.admin.quarterplan;

import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.distm.controller.admin.quarterplan.vo.*;
import cn.iocoder.yudao.module.distm.dal.dataobject.quarterplan.QuarterPlanDO;
import cn.iocoder.yudao.module.distm.service.quarterplan.QuarterPlanService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 配送管理-季度计划")
@RestController
@RequestMapping("/distm/quarter-plan")
@Validated
public class QuarterPlanController {

    @Resource
    private QuarterPlanService quarterPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建配送管理-季度计划")
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:create')")
    public CommonResult<Long> createQuarterPlan(@Valid @RequestBody QuarterPlanSaveReqVO createReqVO) {
        return success(quarterPlanService.createQuarterPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配送管理-季度计划")
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:update')")
    public CommonResult<Boolean> updateQuarterPlan(@Valid @RequestBody QuarterPlanSaveReqVO updateReqVO) {
        quarterPlanService.updateQuarterPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配送管理-季度计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:delete')")
    public CommonResult<Boolean> deleteQuarterPlan(@RequestParam("id") Long id) {
        quarterPlanService.deleteQuarterPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除配送管理-季度计划")
                @PreAuthorize("@ss.hasPermission('distm:quarter-plan:delete')")
    public CommonResult<Boolean> deleteQuarterPlanList(@RequestParam("ids") List<Long> ids) {
        quarterPlanService.deleteQuarterPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配送管理-季度计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:query')")
    public CommonResult<QuarterPlanRespVO> getQuarterPlan(@RequestParam("id") Long id) {
        QuarterPlanDO quarterPlan = quarterPlanService.getQuarterPlan(id);
        return success(BeanUtils.toBean(quarterPlan, QuarterPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配送管理-季度计划分页")
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:query')")
    public CommonResult<PageResult<QuarterPlanRespVO>> getQuarterPlanPage(@Valid QuarterPlanPageReqVO pageReqVO) {
        PageResult<QuarterPlanDO> pageResult = quarterPlanService.getQuarterPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuarterPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配送管理-季度计划 Excel")
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuarterPlanExcel(@Valid QuarterPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuarterPlanDO> list = quarterPlanService.getQuarterPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配送管理-季度计划.xls", "数据", QuarterPlanRespVO.class,
                        BeanUtils.toBean(list, QuarterPlanRespVO.class));
    }

    @PostMapping("/import")
    @Operation(summary = "导入季度计划")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('distm:quarter-plan:export')")
    public CommonResult<QuarterPlanImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<QuarterPlanImportExcelVO> list = ExcelUtils.read(file, QuarterPlanImportExcelVO.class, 0);
        return success(quarterPlanService.importQuarterPlanList(list, updateSupport));
    }

}